package com.gymmsystem.GYMMSystem.service.impl;

import com.gymmsystem.GYMMSystem.entity.Coaches;
import com.gymmsystem.GYMMSystem.mapper.CoachesMapper;
import com.gymmsystem.GYMMSystem.service.CoachesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CoachesServiceImpl implements CoachesService {

    @Autowired
    private CoachesMapper coachesMapper;

    @Override
    public List<Coaches> findAllCoaches() {
        return coachesMapper.findAllCoaches();
    }

    @Override
    public List<Coaches> findCoachesBySpecialty(String specialty) {
        if (specialty == null || specialty.trim().isEmpty()) {
            return null;
        }
        return coachesMapper.findCoachesBySpecialty(specialty);
    }

    @Override
    public List<Coaches> findCoachesByExperienceRange(Integer minExperience, Integer maxExperience) {
        if (minExperience == null || maxExperience == null || minExperience < 0 || maxExperience < minExperience) {
            return null;
        }
        return coachesMapper.findCoachesByExperienceRange(minExperience, maxExperience);
    }

    @Override
    public Coaches findCoachByPhone(String phone) {
        if (phone == null || phone.trim().isEmpty()) {
            return null;
        }
        return coachesMapper.findCoachByPhone(phone);
    }

    @Override
    public boolean updateCoachById(Coaches coach) {
        if (coach == null || coach.getId() == null) {
            return false;
        }

        // 校验是否存在
        if (coachesMapper.selectById(coach.getId()) == null) {
            return false;
        }

        int result = coachesMapper.updateCoachById(coach);
        return result > 0;
    }

    @Override
    public List<Coaches> findExperiencedCoaches() {
        return coachesMapper.findExperiencedCoaches();
    }

    @Override
    public List<Coaches> findCoachesByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return null;
        }
        return coachesMapper.findCoachesByName(name);
    }

    @Override
    public Coaches getCoachById(Long id) {
        if (id == null) {
            return null;
        }
        return coachesMapper.selectById(id);
    }

    @Override
    public Long createCoach(Coaches coach) {
        if (coach == null ||
                coach.getName() == null || coach.getName().trim().isEmpty() ||
                coach.getPhone() == null || coach.getPhone().trim().isEmpty()) {
            return null;
        }

        // 设置默认值
        if (coach.getExperience() == null) {
            coach.setExperience(0);
        }
        if (coach.getCreatedAt() == null) {
            coach.setCreatedAt(LocalDateTime.now());
        }

        int result = coachesMapper.insert(coach);
        return result > 0 ? coach.getId() : null;
    }

    @Override
    public boolean deleteCoachById(Long id) {
        if (id == null) {
            return false;
        }

        int result = coachesMapper.deleteById(id);
        return result > 0;
    }
}